////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package plantool.display.windows.objects {
	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	import flash.display.GradientType;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.text.TextField;
	
	import kiwi.components.Button;
	import kiwi.events.CallbackEvent;
	
	import plantool.display.windows.BaseWindow;
	import plantool.helpers.TextFieldHelper;
	
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	public class WindowHeader extends BaseWindowObject {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		private var _titleTf:TextField		= null;
		private var _closeBtn:Button		= null;
		private var _title:String			= "";
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		public function set title(value:String):void 	{ _title = value; invalidate(); }
		public function get title():String				{ return _title; }
		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function WindowHeader(window:BaseWindow) {
			super(window);
		}
		
		public override function dispose():void {
			//dispose closebutton
			if (_closeBtn) {
				_closeBtn.dispose();
				_closeBtn = null;
			}
			
			//remove tf
			if (_titleTf) {
				if (contains(_titleTf)) removeChild(_titleTf);
				_titleTf = null;
			}
			
			//remove events
			removeEventListener(MouseEvent.MOUSE_DOWN	, onMouse_down);
			removeEventListener(MouseEvent.MOUSE_UP		, onMouse_up);
			
			//super dispose
			super.dispose();
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		private function onMouse_closeBtn(e:CallbackEvent):void {
			switch (e.userData.type) {
				case MouseEvent.CLICK:
					window.dispose();
					break;
			}
		}
		
		private function onMouse_down(e:MouseEvent):void {
			window.startDrag();
		}
		
		private function onMouse_up(e:MouseEvent):void {
			window.stopDrag();
		}
		
		protected override function init():void {
			//create textfield
			_titleTf 				= TextFieldHelper.createInfoTf();
			_titleTf.x 				= 6;
			_titleTf.mouseEnabled 	= false;
			addChild(_titleTf);
			
			//create close button
			_closeBtn			= new Button();
			_closeBtn.width		= 10;
			_closeBtn.height	= 10;
			_closeBtn.x			= width - 15;
			_closeBtn.y			= (height - 10) / 2;
			_closeBtn.mouseHandler = onMouse_closeBtn;
			addChild(_closeBtn);
			
			//set buttonmode
			buttonMode = true;
			
			//listen to mousedown
			addEventListener(MouseEvent.MOUSE_DOWN		, onMouse_down);
			addEventListener(MouseEvent.MOUSE_UP		, onMouse_up);
			
			//super init
			super.init();
		}
		
		public override function draw():void {
			//draw the header
			graphics.clear();
			graphics.beginFill(0);
			graphics.drawRoundRectComplex(0, 0, width, height, height/2, height/2, 0, 0);
			graphics.endFill();
			
			var myMatrix:Matrix = new Matrix();
			myMatrix.createGradientBox(width, height, Math.PI / 2, 0, 0);
			var colors:Array = [0xFFFFFF, 0xFFFFFF];
			var alphas:Array = [.35, 0];
			var ratios:Array = [0, 255];
			
			graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, myMatrix);
			graphics.drawRoundRectComplex(0, 0, width, height, height/2, height/2, 0, 0);
			graphics.endFill();
			
			//set title
			if (_titleTf) {
				_titleTf.text = _title;
			}
			
			//set closeBtn
			if (_closeBtn) {
				_closeBtn.x			= width - 15;
				_closeBtn.y			= (height - 10) / 2;
			}
		}
	}
}